package bingo.rpc.framework.helper;

import bingo.rpc.framework.serialization.common.SerializeType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.InputStream;
import java.util.Properties;

/**
 * property文件操作类
 *
 * @author hujinsheng
 * @project bingo
 * @create 2017-12-11 16:53
 **/
public class PropertyConfigHelper {
    private static final Logger logger = LoggerFactory.getLogger(PropertyConfigHelper.class);
    private static final String PROPERTY_CLASSPATH="zookeeper.properties";
    private static final Properties properties=new Properties();

    //ZK服务地址
    private static String zkService="";
    //ZK session超时时间
    private static int zkSessionTimeout;
    //ZK connection超时时间
    private static int zkConnectionTimeout;
    //序列化算法类型
    private static SerializeType serializeType;
    //每个服务端提供者的Netty的连接数
    private static int channelConnectSize;

    /**
     * 初始化
     */
    static {

        InputStream is=null;
        try{
            is=PropertyConfigHelper.class.getResourceAsStream(PROPERTY_CLASSPATH);
            if (null == is) {
                throw new IllegalStateException("zookeeper.properties can not found in the classpath.");
            }
            properties.load(is);
            zkService=properties.getProperty("zk_service");
            zkSessionTimeout=Integer.parseInt(properties.getProperty("zk_sessionTimeout"));
            zkConnectionTimeout=Integer.parseInt(properties.getProperty("zk_connectionTimeout"));
            channelConnectSize=Integer.parseInt(properties.getProperty("channel_connect_size"));
            serializeType=SerializeType.queryByType(properties.getProperty("serialize_type"));
            if (serializeType == null) {
                throw new RuntimeException("serializeType is null");
            }


        }catch (Exception e){
            e.printStackTrace();
        }

    }



    public static String getZkService() {
        return zkService;
    }

    public static int getZkSessionTimeout() {
        return zkSessionTimeout;
    }

    public static int getZkConnectionTimeout() {
        return zkConnectionTimeout;
    }

    public static SerializeType getSerializeType() {
        return serializeType;
    }

    public static int getChannelConnectSize() {
        return channelConnectSize;
    }
}
